package com.ruoyi.web.controller.facilities;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.EscapeRefugeFacilities;
import com.ruoyi.system.domain.LabourProtectArticles;
import com.ruoyi.system.service.LabourProtectArticlesService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 劳动防护用品和装备(LabourProtectArticles)表控制层
 *
 * @author makejava
 * @since 2022-11-16 15:25:23
 */
@RestController
@RequestMapping("labour/protect/articles")
public class LabourProtectArticlesController {
    /**
     * 服务对象
     */
    @Resource
    private LabourProtectArticlesService labourProtectArticlesService;

    /**
     * 分页查询
     *
     * @param labourProtectArticles 筛选条件
     * @return 查询结果
     */
    @GetMapping("list")
    public TableDataInfo queryByPage(LabourProtectArticles labourProtectArticles) {
        startPage();
        List<LabourProtectArticles> list = labourProtectArticlesService.selectList(labourProtectArticles);
        return getDataTable(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<LabourProtectArticles> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.labourProtectArticlesService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param labourProtectArticles 实体
     * @return 新增结果
     */
    @PostMapping("add")
    @PreAuthorize("@ss.hasPermi('labour:protect:articles:add')")
    public ResponseEntity<LabourProtectArticles> add(@Validated @RequestBody LabourProtectArticles labourProtectArticles) {
        return ResponseEntity.ok(this.labourProtectArticlesService.insert(labourProtectArticles));
    }

    /**
     * 编辑数据
     *
     * @param labourProtectArticles 实体
     * @return 编辑结果
     */
    @PutMapping("edit")
    @PreAuthorize("@ss.hasPermi('labour:protect:articles:edit')")
    public ResponseEntity<LabourProtectArticles> edit(@Validated @RequestBody LabourProtectArticles labourProtectArticles) {
        return ResponseEntity.ok(this.labourProtectArticlesService.update(labourProtectArticles));
    }

    /**
     * 删除数据
     *
     * @return 删除是否成功
     */
    @DeleteMapping("delete")
    @PreAuthorize("@ss.hasPermi('labour:protect:articles:delete')")
    public ResponseEntity<Boolean> deleteById(@Validated @RequestBody LabourProtectArticles labourProtectArticles) {
        return ResponseEntity.ok(this.labourProtectArticlesService.deleteById(labourProtectArticles.getId()));
    }


    /**
     * 设置请求分页数据
     */
    protected void startPage() {
        PageUtils.startPage();
    }

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected TableDataInfo getDataTable(List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    @Log(title = "作业场地防护设施(", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @PreAuthorize("@ss.hasPermi('labour:protect:articles:importData')")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<LabourProtectArticles> util = new ExcelUtil<>(LabourProtectArticles.class);
        List<LabourProtectArticles> labourProtectArticless = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = labourProtectArticlesService.importExcel(labourProtectArticless, updateSupport, operName);
        return AjaxResult.success(message);
    }


    @Log(title = "作业场地防护设施(", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @PreAuthorize("@ss.hasPermi('labour:protect:articles:export')")
    public void export(HttpServletResponse response, LabourProtectArticles labourProtectArticles) {
        List<LabourProtectArticles> list = labourProtectArticlesService.selectList(labourProtectArticles);
        ExcelUtil<LabourProtectArticles> util = new ExcelUtil<>(LabourProtectArticles.class);
        util.exportExcel(response, list, "作业场地防护设施(");
    }


    @PostMapping("/importTemplate")
    @PreAuthorize("@ss.hasPermi('labour:protect:articles:importTemplate')")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<LabourProtectArticles> util = new ExcelUtil<>(LabourProtectArticles.class);
        util.importTemplateExcel(response, "作业场地防护设施(");
    }

    public String getUsername() {
        return "";
    }

}

